package com.sxkiler.demo.test.elasticsearch;

import org.frameworkset.elasticsearch.client.ClientInterface;

import java.util.List;
import java.util.Map;

public class ESDataUtil {

    /**
     * 指定indexType
     * @param clientInterface
     * @param indexType
     * @param indexName
     * @return
     */
    public static Map queryDataById(ClientInterface clientInterface, String indexName,String indexType,String docId){
        return clientInterface.getDocument(indexName,indexType,docId,Map.class);
    }
    /**
     * 指定indexType
     * @param clientInterface
     * @param indexName
     * @return
     */
    public static Map queryDataById(ClientInterface clientInterface,String indexName,String docId){
        return queryDataById(clientInterface,indexName,"_doc",docId);
    }
    /**
     * 指定indexType
     * @param clientInterface
     * @param indexType
     * @param indexName
     * @param objList
     * @param docIdKey
     * @return
     */
    public static String addData(ClientInterface clientInterface,String indexType, String indexName, List<Map> objList,String docIdKey){
        return clientInterface.addDocumentsWithIdKey(indexName,indexType,objList,docIdKey);
    }

    /**
     *
     * @param clientInterface
     * @param indexName
     * @param objList
     * @param docIdKey
     * @return
     */
    public static String addData(ClientInterface clientInterface, String indexName, List<Map> objList,String docIdKey){
        //addDateDocumentWithId 指定了indexType
        //addDocumentWithId 没有指定indexType
        return clientInterface.addDocumentsWithIdKey(indexName,"_doc",objList,docIdKey);
    }

    /**
     * 更新数据
     * @param clientInterface
     * @param indexType 索引类型
     * @param indexName 索引名称
     * @param objList   需要更新的数据列表
     * @param docIdKey  主键id
     * @return
     */
    public static String updateData(ClientInterface clientInterface,String indexType, String indexName, List<Map> objList,String docIdKey){
        //addDateDocumentWithId 指定了indexType
        //addDocumentWithId 没有指定indexType
        return clientInterface.updateDocumentsWithIdKey(indexName,indexType,objList,docIdKey);
    }

    /**
     * 更新数据，默认indexType为_doc
     * @param clientInterface 索引类型
     * @param indexName 索引名称
     * @param objList  需要更新的数据列表
     * @param docIdKey 主键id
     * @return
     */
    public static String updateData(ClientInterface clientInterface, String indexName, List<Map> objList,String docIdKey){
        return clientInterface.updateDocumentsWithIdKey(indexName,"_doc",objList,docIdKey);
    }


    /**
     *
     * @param clientInterface
     * @param indexName
     * @param ids id列表
     * @return
     */
    public static String deleteData(ClientInterface clientInterface, String indexName, String [] ids){
        //addDateDocumentWithId 指定了indexType
        //addDocumentWithId 没有指定indexType
        return clientInterface.deleteDocuments(indexName,"_doc",ids);
    }

    /**
     *
     * @param clientInterface
     * @param indexName
     * @param indexType 索引类型
     * @param ids id列表
     * @return
     */
    public static String deleteData(ClientInterface clientInterface, String indexName, String indexType, String [] ids){
        //addDateDocumentWithId 指定了indexType
        //addDocumentWithId 没有指定indexType
        return clientInterface.deleteDocuments(indexName,indexType,ids);
    }

}
